﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Routing;

namespace Arbalest.Web.RouteHandlers
{
    /// <summary>
    /// Reference: http://patrickdesjardins.com/blog/how-to-create-a-httphandler-with-asp-mvc
    /// </summary>
    public class ImageHandler : IRouteHandler
    {
        #region IRouteHandler Members

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var routeValues = requestContext.RouteData.Values;
            if (routeValues.ContainsKey("UniqueIdentifier"))
            {
                ////Do something with the parameter (UniqueIdentifier) to get the image on the server
                //string serverPathToImageToOutput = //What ever you want like database call or file server algo;

                ////Start a new Response
                //requestContext.HttpContext.Response.Clear();

                ////Response type will be the same as the one requested
                //requestContext.HttpContext.Response.ContentType = 
                //    GetContentType(requestContext.HttpContext.Request.Url.ToString());

                ////We buffer the data to send back until it's done
                //requestContext.HttpContext.Response.BufferOutput = true;
                //Image image = Image.FromStream(new FileStream(serverPathToImageToOutput, FileMode.Open));
                //image.Save(requestContext.HttpContext.Response.OutputStream, ImageFormat.Png);
                //requestContext.HttpContext.Response.End();
                return null;
            }
            else
            {
                return null;
            }
        }

        #endregion
    }
}